import { Toast } from 'antd-mobile'
import _ from 'lodash'
import { createAction, NavigationActions, Storage, URL } from '../utils'
import * as authService from '../services/auth'
import { get, deletes, post, puts, whereGet } from '../utils/fetch'

export default {
  namespace: 'app',
  state: {
    login: false,
    loading: true,
    fetching: false,
    newTimeList: null,
    collectionList: null,
    LoveList: null
  },
  reducers: {
    updateState(state, { payload }) {
      return { ...state, ...payload }
    },
    saveNewTime(state, { payload }) {
      return {
        ...state,
        newTimeList: payload.payload
      }
    },
    saveLove(state, { payload }) {
      return {
        ...state,
        LoveList: payload.payload
      }
    },
    saveCollection(state, { payload }) {
      return {
        ...state,
        collectionList: payload.payload
      }
    }
  },
  effects: {
    * loadStorage(action, { call, put }) {
      yield put(createAction('updateState')({ loading: false }))
    },
    * loadNewTimeList({ payload }, { call, put }) {
      const data = yield call(() => get(`${URL}1/classes/Travels?order=-createdAt&include=user`))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail('加载最新列表失败', 1)
        } else {
          yield put({ type: 'loadStorage' })
          yield put(createAction('saveNewTime')({ payload: data.results }))
          yield put({ type: 'loadLove' })
        }
      } else {
        Toast.fail('加载失败', 1)
      }
    },
    * loadLove({ payload }, { call, put }) {
      const data = yield call(() => get(`${URL}1/classes/Travels?order=-love&include=user`))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail('加载关注排行列表失败！！！')
        } else {
          yield put(createAction('saveLove')({ payload: data.results }))
        }
      } else {
        Toast.fail('加载失败', 1)
      }
    },
    * loveShare({ payload }, { call, put }) {
      const { travelsId, userID, type } = payload
      if (type === 'ADD') {
        yield put({ type: 'addLove', payload: { travelsId, userID } })
      } else {
        yield put({ type: 'deleteLove', payload: { travelsId, userID } })
      }
    },
    * loadCollection({ payload }, { call, put, select }) {
      const { user } = yield select(state => state.login)
      const userId = user.objectId
      console.log('test user collection', user)
      if (_.isObject(user)) {
        const data = yield call(() => whereGet(`${URL}1/classes/Travels?where={"collection":"${userId}"}`))
        if (data) {
          if (_.has(data, 'code')) {
            Toast.fail('获取当前用户收藏列表失败', 2)
          } else {
            yield put(createAction('saveCollection')({ payload: data.results }))
          }
        } else {
          Toast.fail('加载当前用户收藏列表失败！！！')
        }
      } else {

      }
    },
    * addLove({ payload }, { call, put }) {
      const { travelsId, userID } = payload
      const data = yield call(() => puts(`${URL}1/classes/Travels/${travelsId}`, {
        love: {
          __op: 'AddUnique',
          objects: [userID]
        }
      }))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail('关注失败', 2)
        } else {
          yield put({ type: 'loadNewTimeList' })
          Toast.success('关注成功')
        }
      }
    },
    * deleteLove({ payload }, { call, put }) {
      const { travelsId, userID } = payload
      const data = yield call(() => puts(`${URL}1/classes/Travels/${travelsId}`, {
        love: {
          __op: 'Remove',
          objects: [userID]
        }
      }))
      if (data) {
        console.log('test data delete love', data)
        if (_.has(data, 'code')) {
          Toast.fail('取消关注失败', 2)
        } else {
          yield put({ type: 'loadNewTimeList' })
          Toast.success('取消关注成功')
        }
      }
    },
    * collectShare({ payload }, { call, put }) {
      const { travelsId, userID, type } = payload
      if (type === 'ADD') {
        yield put({ type: 'addCollect', payload: { travelsId, userID } })
      } else {
        yield put({ type: 'deleteCollect', payload: { travelsId, userID } })
      }
    },
    * addCollect({ payload }, { call, put }) {
      const { travelsId, userID } = payload
      const data = yield call(() => puts(`${URL}1/classes/Travels/${travelsId}`, {
        collection: {
          __op: 'AddUnique',
          objects: [userID]
        }
      }))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail('收藏失败', 2)
        } else {
          yield put({ type: 'loadNewTimeList' })
          yield put({ type: 'loadCollection' })
          Toast.success('收藏成功', 2)
        }
      }
    },
    * deleteCollect({ payload }, { call, put }) {
      const { travelsId, userID } = payload
      const data = yield call(() => puts(`${URL}1/classes/Travels/${travelsId}`, {
        collection: {
          __op: 'Remove',
          objects: [userID]
        }
      }))
      if (data) {
        if (_.has(data, 'code')) {
          Toast.fail('取消收藏失败', 2)
        } else {
          yield put({ type: 'loadNewTimeList' })
          yield put({ type: 'loadCollection' })
          Toast.success('取消收藏成功', 2)
        }
      }
    }
  },
  subscriptions: {
    setup({ dispatch }) {
      dispatch({ type: 'loadNewTimeList' })
    }
  }
}
